package com.asm.controller.api;

import com.asm.dto.PageResult;
import com.asm.dto.Result;
import com.asm.service.BookService;
import com.asm.vo.sys.BookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/**
 * @author tanzujie
 */
@RestController
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     * 分页查询所有工单
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/books")
    public ResponseEntity<PageResult<BookVo>> getAllBooks(Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", bookService.findAllBooks(pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据Id获取工单
     *
     * @param id
     * @return
     */
    @GetMapping("/book/{id}")
    public ResponseEntity<Result<BookVo>> getBookById(@PathVariable("id") Long id) {
        return new ResponseEntity(new Result().ok("success", bookService.findBookById(id)), HttpStatus.OK);
    }

    /**
     * 根据工单类型分页查询工单
     *
     * @param workerid
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/books/workerid/{workerid}")
    public ResponseEntity<PageResult<BookVo>> getBooksByType(@PathVariable("workerid") Long workerid, Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", bookService.findBookByWorkerId(workerid, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据工单大小分页查询工单
     *
     * @param contractid
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/books/contractid/{contractid}")
    public ResponseEntity<PageResult<BookVo>> getBooksBySize(@PathVariable("contractid") Long contractid, Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", bookService.findBookByContractId(contractid, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 增加工单
     *
     * @param bookVo
     * @return
     */
    @PostMapping("/book/add")
    public ResponseEntity<Result> addBook(@RequestBody BookVo bookVo) {
        bookService.addBook(bookVo);
        return new ResponseEntity(new Result().ok("add success"), HttpStatus.OK);
    }

    /**
     * 更新工单
     *
     * @param bookVo
     * @return
     */
    @PutMapping("/book/update")
    public ResponseEntity<Result> updateBook(@RequestBody BookVo bookVo) {
        bookService.updateBook(bookVo);
        return new ResponseEntity(new Result().ok("update success"), HttpStatus.OK);
    }

    /**
     * 根据Id删除工单
     *
     * @param id
     * @return
     */
    @DeleteMapping("/book/delete/{id}")
    public ResponseEntity<Result> deleteBook(@PathVariable("id") Long id) {
        bookService.deleteBook(id);
        return new ResponseEntity(new Result().ok("delete success"), HttpStatus.OK);
    }
}
